<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台控制器演示。
 *
 **/
namespace Home\Controller;

use Vendor\Page;
class IndexController extends ComController
{
    public function index()
    {
        // 丹唐簡介
        $article = M('article');
        $photos = M('photos');
        $product = M('product');
        $articleOrder = "aid DESC";
        $about = $article->where("sid = ".JIANJIE)->limit(1)->order($articleOrder)->select();
        $this->assign('about',$about[0]);
        $this->assign('aboutSid',JIANJIE);
        // 轮播图
        $banner = $photos->where("deleted = 0 AND category = 1")->order("id DESC")->select();
        $this->assign('banner',$banner);
        // 产品图
        $product = $product->where("deleted = 0")->order("id DESC")->select();
        $this->assign('product',$product);
        // 新闻媒体
        $xwmt = $article->field("aid,title,t")->where("sid = ".XWMT)->limit(8)->order($articleOrder)->select();
        $this->assign('xwmt',$xwmt);
        $this->assign('xwmtId',XWMT);
        // 行业动态
        $hydt = $article->field("aid,title,t")->where("sid = ".HYDT)->limit(8)->order($articleOrder)->select();
        $this->assign('hydt',$hydt);
        $this->assign('hydtId',HYDT);
        $this->assign('active',1);
        if(isMobile()){     
            $this->display("Mobile/index");
        }else{
//        pp($hydt);exit();
        $this->display();
        }
    }
    //列表
    public function news($p = 1){

        $p = intval($p) > 0 ? $p : 1;
        $article = M('article as a');
        if(!empty($_GET['sid'])){
            $where = "sid=".$_GET['sid'];
            $this->assign('sid',$_GET['sid']);
        }else{
            $where['sid'] = ['in',''.XWMT.','.HYDT.','.GSXW.','.CJWT.''];
        }
        $orderby = "a.aid DESC";
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $field = "a.aid,a.title,a.description,a.thumbnail,a.t,{$prefix}category.name,{$prefix}category.dir";
        $count = $article->where($where)->count();
        $list = $article->field($field)
            ->where($where)
            ->order($orderby)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->limit($offset . ',' . $pagesize)
            ->select();
        //echo $m->getlastsql();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('active',2);
        if(isMobile()){
            $this->display("Mobile/news");
        }else{
            $this->display();
        }
    }
    //单页
    public function page($aid){

        $aid = intval($aid);

        $prefix = C('DB_PREFIX');
        $articleDb = M('article as a');
        // 点击量加1
        $articleDb->where("aid=$aid")->setInc('n',1);
        // 文章
        $field = "a.aid,a.title,a.description,a.content,a.thumbnail,a.t,a.n,{$prefix}category.name";
        $article = $articleDb->field($field)
                ->where('aid='.$aid)
                ->join("{$prefix}category ON {$prefix}category.id = a.sid")
                ->find();
        $this->assign('article',$article);
        $this->assign('nav',$aid);
        $this->assign('sid',$article['sid']);
//        pp($article);exit();
        // 上一篇
        $upWhere['aid'] = ['gt',$aid];
        $up   = $this->articleDetail($upWhere,'aid');
        // 下一篇
        $nextWhere['aid'] = ['lt',$aid];
        $next = $this->articleDetail($nextWhere,'aid desc');
        $this->assign('up',$up);
        $this->assign('next',$next);
        if(isMobile()){
            $this->display("Mobile/page");
        }else{
            $this->display();
        }
    }

    public function articleDetail($where,$order)
    {
        $where['sid'] = ['in',''.XWMT.','.HYDT.','.GSXW.','.CJWT.''];
        $articleDb = M('article as a');
        $data = $articleDb
                ->field("aid,title,sid")
                ->where($where)
                ->order($order)
                ->find();
        return $data;
    }

    // 商品描述
    public function product(){
        // 产品简介
        $article = M('article');
        $articleOrder = "aid DESC";
        $cpjj = $article->where("sid = ".CPJJ)->limit(8)->order($articleOrder)->select();
        $this->assign('cpjj',$cpjj[0]);
        $this->assign('cpjjId',CPJJ);
        // 商品列表
        $productDB = M('product');
        $where = "deleted = 0";
        if($_GET['tid']){
            $where .= " AND pro_type = ".$_GET['tid'];
            $this->assign('pro_type',$_GET['tid']);
        }
        $product = $productDB->where($where)->order("id DESC")->select();
        $this->assign('product',$product);
        $this->assign('active',3);
        // 上一篇
        $upWhere['aid'] = ['gt',$aid];
        $up   = $this->articleDetail($upWhere,'aid');
        // 下一篇
        $nextWhere['aid'] = ['lt',$aid];
        $next = $this->articleDetail($nextWhere,'aid desc');
        $this->assign('up',$up);
        $this->assign('next',$next);

        if(isMobile()){
            $this->display("Mobile/product");
        }else{
        $this->display();
         }
    }
    public function detail($id){
        $pro_id = intval($id);
        if(!empty($pro_id)){
            $productDB = M('product');
            $where = "deleted = 0 AND id = $pro_id";
            $detail = $productDB->where($where)->find();
//            pp($detail);exit();
            $this->assign('detail',$detail);
            if(isMobile()){
            $this->display("Mobile/detail");
        }else{
        $this->display();
         }
        }
    }

    public function contact(){

        $this->assign('active',4);
        if(isMobile()){
            $this->display("Mobile/contact");
        }else{
        $this->display();
         }
    }

    public function message(){

        $data['name'] = isset($_POST['name']) ? $_POST['name'] : false;
        $data['content'] = isset($_POST['content']) ? $_POST['content'] : '';
        $data['mobile'] = isset($_POST['tel']) ? $_POST['tel'] : false;
        $data['ctime']= date('Y-m-d H:i:s');
        //var_dump($data);exit;
        if (!$data['name'] or !$data['mobile']) {
            $this->ajaxReturn(["code"=>400,"msg"=>"警告！留言信息不完整！"]);
        }
        if (M('message')->add($data)) {
            $this->ajaxReturn(["code"=>200,"msg"=>"提交成功！"]);
        } else {
            $this->ajaxReturn(["code"=>400,"msg"=>"提交失败！"]);
        }
    }
    public function search(){

        $keyword = !empty($_GET['k']) ? $_GET['k'] : "";
        if(!empty($keyword)){
            $arr = $this->searchBy($keyword);
            if(!empty($arr)){
                $this->assign('num',count($arr['list']));
                $this->assign('list',$arr['list']);
                $this->assign('page',$arr['page']);
                $this->assign('keyword',$keyword);
            }
        }
        if(isMobile()){
            $this->display("Mobile/search");
        }else{$this->display();}
        
    }

    public function about(){

        $this->assign('active',5);
        if(isMobile()){
            $this->display("Mobile/about");
        }else{
            $this->display();
        }
        
    }
    public function article(){
        $this->assign('active',3);
        $this->display("Mobile/article");
       
    }
    public function article1(){
        $this->assign('active',3);
        if(isMobile()){
            $this->display("Mobile/article1");
        }else{
        $this->display();
         }
    }
    public function article2(){
        $this->assign('active',3);
        if(isMobile()){
            $this->display("Mobile/article2");
        }else{
        $this->display();
         }
    }

    public function about2(){
        $this->assign('active',5);
        if(isMobile()){
            $this->display("Mobile/about2");
        }else{
        $this->display();
         }
    }
    public function about3(){
        $this->assign('active',5);
        if(isMobile()){
            $this->display("Mobile/about3");
        }else{
        $this->display();
         }
    }

}